<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
      body {
        text-align: center;
      }
      ul {
        list-style-type: none;
      }
      li {
        padding-bottom: 20px;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <h3>搜索列表</h3>
      <label for=""
        ><input type="text" placeholder="请输入名字搜索" v-model="searchStr"
      /></label>
      <div>
        <h4>排序</h4>
        <button @click="changeOrder(1)">升序</button>
        <button @click= "changeOrder(2)">降序</button>
        <button @click= "changeOrder(0)">不排序</button>
      </div>
      <ul>
        <li v-for="(item, index) in searchRes">
          {{index +
          1}}-姓名:{{item.name}}-年龄:{{item.age}}-联系方式:{{item.phone}}
        </li>
      </ul>
    </div>

    <script src="../js/vue.js"></script>

    <script>
      const app = Vue.createApp({
        data() {
          return {
            persons: [
              { name: "张三", sex: "女", age: 19, phone: "18921212121" },
              { name: "李四", sex: "男", age: 29, phone: "18921221121" },
              { name: "王五", sex: "女", age: 39, phone: "18921788721" },
              { name: "赵六", sex: "男", age: 49, phone: "18921556121" },
              { name: "李四", sex: "男", age: 29, phone: "18921221121" },
              { name: "王五", sex: "女", age: 39, phone: "18921788721" },
              { name: "李四", sex: "男", age: 9, phone: "18921221121" },
              { name: "王五", sex: "女", age: 39, phone: "18921788721" },
              { name: "王五", sex: "女", age: 39, phone: "18921788721" },
              { name: "赵六", sex: "男", age: 49, phone: "18921556121" },
              { name: "李思思", sex: "男", age: 29, phone: "18921221121" },
              { name: "张三", sex: "女", age: 19, phone: "18921212121" },
              { name: "李四", sex: "男", age: 39, phone: "18921221121" },
              { name: "王五", sex: "女", age: 39, phone: "18921788721" },
              { name: "赵六", sex: "男", age: 49, phone: "18921556121" },
              { name: "李四", sex: "男", age: 29, phone: "18921221121" },
              { name: "王五", sex: "女", age: 39, phone: "18921788721" },
              { name: "李四", sex: "男", age: 29, phone: "18921221121" },
              { name: "王五", sex: "女", age: 39, phone: "18921788721" },
              { name: "王五", sex: "女", age: 39, phone: "18921788721" },
              { name: "赵六", sex: "男", age: 49, phone: "18921556121" },
              { name: "李五五", sex: "男", age: 29, phone: "18921221121" },
            ],
            //关联搜索框
            searchStr: "",
            //排序 0不排序 1升序 2降序
            orderType: 0,
          };
        },
        computed: {
          searchRes() {
            //1. 取出相关属性
            const {persons, searchStr, orderType} = this;
            //2. 定义过滤数组
            let arr = [...persons];
            //3. 过滤出符合条件的数据
            if (searchStr.trim()) {
              console.log(searchStr);
              arr = arr.filter((value) => value.name.indexOf(searchStr) != -1);
            }

            //4. 排序
            if (orderType) {
              arr.sort((a, b) =>
                orderType == 1 ? a.age - b.age : b.age - a.age
              );
            }

            return arr;
          },
        },

        methods: {
          changeOrder(orderType){
            console.log(456);
            this.orderType = orderType;
          }
        },
      }).mount("#app");
    </script>
  </body>
</html>
